from django.shortcuts import render
from django.http import HttpResponse
import pyecharts
from . import ana
import redis
import json

douban = ana.DouBanAna()
r = redis.Redis(host='192.168.92.132', port=6379, password = '1', db = '0')

REMOTE_HOST = "https://pyecharts.github.io/assets/js"

# Create your views here.

def index(request):

    return HttpResponse('OK')

def how_many_reviews_are_there_per_movie(request):
    bar1 = pyecharts.Bar('每部电影评论人数')

    if r.exists('how_many_reviews_are_there_per_movie'):

        info_dict1 = json.loads(r.get('how_many_reviews_are_there_per_movie').decode())

        bar1.add('test',list(info_dict1.keys()), list(info_dict1.values()))

    else:


        info1 = douban.how_many_reviews_are_there_per_movie()
        bar1.add('test',info1.index, info1.values)

    info2 = douban.total_thumb_up_per_film()

    #bar1 = pyecharts.Bar('每部电影评论人数')

    bar2 = pyecharts.Bar('每部电影的Like次数')

    bar2.add('test', info2.index, info2.values)


    context = dict(
        
        myechart1=bar1.render_embed(),
        myechart2=bar2.render_embed(),
        
        host=REMOTE_HOST,
        
        script_list=bar1.get_js_dependencies()
        )

    print(bar1.get_js_dependencies())

    return render(request, 'douban/bootcss.html', context)

    
